package com.zlzxm.shoesmarket.viewmodel;

import androidx.lifecycle.MutableLiveData;

import com.zlzxm.shoesmarket.globle.Constant;
import com.zlzxm.shoesmarket.globle.UserManager;
import com.zlzxm.shoesmarket.net.BaseCallBack;
import com.zlzxm.shoesmarket.net.repository.WareRepository;
import com.zlzxm.shoesmarket.net.respone.RpRepository;
import com.zlzxm.shoesmarket.util.RxAndroidHelp;
import com.zlzxm.shoesmarket.view.adapter.entity.EntityRepository;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zlz
 * on  4/14/21
 */
public class VMRepositoryManager extends BaseViewModel {

    private static final String TAG = "VMRepositoryManager";


    WareRepository wareRepository = new WareRepository();

    private MutableLiveData<List<EntityRepository>> liveRepository = new MutableLiveData<>();

    public MutableLiveData<List<EntityRepository>> getLiveRepository() {
        return liveRepository;
    }


    public void requestRepository(){

        if(!UserManager.getInstance().isLogin()){

            return;
        }


        wareRepository.ware(UserManager.getInstance().getId())
                .compose(RxAndroidHelp.turn())
                .subscribe(new BaseCallBack<List<RpRepository>>() {
                    @Override
                    protected void succeed(String msg, List<RpRepository> rpRepositories) {

                        liveRepository.setValue(createData(rpRepositories));
                    }

                    @Override
                    protected void failed(String msg) {

                        liveErrorToast.setValue(msg);
                    }

                    @Override
                    protected void notLogin() {

                    }
                }, throwableCallback);



    }


//<editor-fold defaultState="collapsed" desc="测试">

    private List<EntityRepository> createData(List<RpRepository> sorce){

        List<EntityRepository> list = new ArrayList<>();

        for (RpRepository repository:sorce) {

            EntityRepository entityRepository = new EntityRepository();
            entityRepository.setId(repository.getId());
            entityRepository.setBrokerage(repository.getCharge());
            entityRepository.setFirst(true);
            switch (repository.getCktype()){

                case 0:
                    entityRepository.setType(Constant.RepositoryType.NORMAL);
                    break;
                case 1:
                    entityRepository.setType(Constant.RepositoryType.QUICK);
                    break;
            }
            entityRepository.setName(repository.getCkname());
            list.add(entityRepository);
        }

        return list;

    }


// </editor-fold>
}
